#version 150

#moj_import <fog.glsl>

uniform sampler2D Sampler0;

uniform vec4 ColorModulator;
uniform float FogStart;
uniform float FogEnd;
uniform vec4 FogColor;

in float vertexDistance;
in vec4 ssao;
in vec2 texCoord0;
in vec3 normal;
in vec4 ViewPosition;

out vec4 fragColor;

void main() {
    vec4 diffuse = texture(Sampler0,texCoord0);
    if(diffuse.a < 0.1)discard;
    //light scene
    vec4 lightColor = vec4(1.0, 1.0, 1.0, 1.0);
    vec4 lightPosition = vec4(0.0, 0.0, 0.0, 1.0);
    //vectors
    vec3 lightDirection = normalize(lightPosition.xyz - ViewPosition.xyz);
    vec3 viewDirection = normalize(ViewPosition.xyz);
    vec3 midDirection = normalize(lightDirection - viewDirection);
    //other arguments
    float viewDistance = vertexDistance * vertexDistance;
    float gloss = 256.0;
    //ambient diffuse and specular
    vec4 ambiColor = diffuse       * ssao;
    vec4 diffColor = diffuse       * (lightColor / viewDistance) *     max(0.0 ,dot(lightDirection, normal));
    vec4 specColor = diffuse * 2.0 * (lightColor / viewDistance) * pow(max(0.0 ,dot(midDirection,   normal)),gloss);
    //combined color
    vec4 color = (ambiColor + diffColor + specColor) * ColorModulator;
    fragColor = linear_fog(color, vertexDistance, FogStart, FogEnd, FogColor);
}
